import React, { Fragment } from 'react';
import { ColumnsType } from 'antd/es/table';
import { Table, Button, Space, message } from 'antd';
import { connect } from 'umi';
import MfgMtlSearch from './MfgMtlSearch';
import { momentStr } from '@/utils/utils';
import request from '@/utils/request';

const apiURL = '/api/services/app/PartTran/';
//数量调整
interface ReturnDto {
  key: string;
  tranReference: boolean;
  jobNum: string;
  assemblySeq: number;
  jobSeq: number;
  partNum: string;
  partDescription: string;
  wareHouseCode: string;
  binNum: string;
  lotNum: string;
  tranQty: number;
  um: number;
  tranDate: string;
  empID: string;
  id: string;
}
class MfgMtlReturn extends React.Component<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      formVals: {},
      dataSource: [],
      defaultParams: {},
      params: {},
      selectedEmp: '',
      selectedKeys: [],
      selectedRows: [],
      hideSearchItem: false,
      pageIndex: 1,
      pageSize: 20
    };
  }

  printArea: any = React.createRef();
  componentDidMount() {
    this.query();
  }
  query = () => {
    //接口
    const { params, pageSize, pageIndex } = this.state;
    this.setState({ loading: true });
    params.tranType = "STK_MTL"
    params.skipCount = (pageIndex - 1) * pageSize;
    params.maxResultCount = pageSize;
    request(apiURL + 'GetReturnMtl', {
      method: 'GET',
      params
    }).then((response) => {
      var resp = response.result;
      this.setState({ dataSource: resp.items, totalCount: resp.totalCount,loading:false });
    });
  };
  /**
   * 搜索
   */
  handleSearch = (values: any) => {
    this.setState(
      {
        params: { ...values },
      },
      this.query,
    );
  };
  /**
   * @param selectedKeys 选择单元格
   */
  handleRowSelect = (selectedKeys: any, selectedRows: any) => {
    this.setState({
      selectedKeys,
      selectedRows,
    });
  };
  /**
   *
   * @param values 退料
   */
  handleSubmit = async () => {
    const { dispatch } = this.props;
    const { selectedRows } = this.state;
    if (selectedRows.length <= 0) {
      return message.warning('请选择记录');
    }
    var issuedList: any[] = [];
    selectedRows.forEach((element: ReturnDto) => {
      var tempObj: any = {
        id: element.id,
        jobNum: element.jobNum,
        asmSeq: element.assemblySeq,
        mtlSeq: element.jobSeq,
        returnQty: element.tranQty,
        whseCode: element.wareHouseCode,
        binNum: element.binNum,
        lotNum: element.lotNum,
        empID: element.empID,
      };
      issuedList.push(tempObj);
    });
    await dispatch({
      type: 'Abp/returnMtl',
      payload: issuedList,
    });
    this.query();
  };
  render() {
    const { selectedKeys, dataSource, pageSize, totalCount,loading } = this.state;
    const rowSelection = {
      selectedRowKeys: selectedKeys,
      onChange: this.handleRowSelect,
    };
    return (
      <Fragment>
        <MfgMtlSearch onSearch={this.handleSearch} hideSearchItem={{ hideSearchItem: true }}>
          <Space>
            <Button type="primary" onClick={this.handleSubmit}>
              确认退料
            </Button>
            <Button danger>打印</Button>
          </Space>
        </MfgMtlSearch>
        <Table
          rowKey="id"
          bordered
          loading={loading}
          dataSource={dataSource}
          columns={this.columns}
          rowSelection={rowSelection}
          scroll={{ x: 'max-content' }}
          pagination={{
            onChange: page => {
              this.setState({ pageIndex: page }, () => {
                this.query();
              });
            },
            pageSize,
            total: totalCount,
          }}
        />
      </Fragment>
    );
  }
  columns: ColumnsType<ReturnDto> = [
    {
      title: '领料单号',
      dataIndex: 'tranReference',
      width: 100,
    },
    {
      title: '工单号',
      dataIndex: 'jobNum',
      width: 80,
    },
    {
      title: '半成品号',
      dataIndex: 'assemblySeq',
      align: 'center',
      width: 100,
    },
    {
      title: '物料号',
      dataIndex: 'jobSeq',
      align: 'center',
      width: 100,
    },
    {
      title: '物料编码',
      dataIndex: 'partNum',
      width: 120,
    },
    {
      title: '物料',
      dataIndex: 'partDescription',
      ellipsis: true,
      width: 300,
    },
    {
      title: '仓库',
      dataIndex: 'wareHouseCode',
      width: 100,
    },
    {
      title: '库位',
      dataIndex: 'binNum',
      width: 100,
    },
    {
      title: '批次',
      dataIndex: 'lotNum',
      width: 100,
    },
    {
      title: '发料量',
      dataIndex: 'tranQty',
      width: 100,
    },
    {
      title: '单位',
      dataIndex: 'um',
      width: 100,
    },
    {
      title: '领料人',
      dataIndex: 'empID',
      width: 100,
    },
    {
      title: '领料时间',
      width: 120,
      dataIndex: 'tranDate',
      render: (val: any) => momentStr(val),
    },
  ];
}
export default connect(({ Abp }: API.IConnectState) => ({
  response: Abp.response,
}))(MfgMtlReturn);
